package com.liu.community.dto;/*
 *@Author:Liu Xing-zhi
 *Description:  分页实体类
 *Date:Crated in 9:30 2021/10/9
 */

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
@Data
public class PageDto<T> {

    public static final Integer PAGE_SIZE =5;

    private List<T> data;//用于封装分页数据

    private  boolean showPreviousPage; //是否展示前一页
    private  boolean showFirstPage;//是否展示第一页
    private  boolean showNextPage;//是否展示下一页
    private  boolean showLastPage;//是否展示最后一页

    private Integer pageSize=PAGE_SIZE;//当前页展示的数据量
    private  Integer currentPage;// 当前页
    private  Integer dataTotalCount;//总记录数
    private  Integer pageTotalCount;//总页码

    private  List<Integer> pages = new ArrayList<>();

    public void setPageMessage(Integer currentPage ,Integer pageSize,Integer dataTotalCount,PageDto<T> pageDto){
        //求总页码数
          pageTotalCount = dataTotalCount / pageSize;
        if(dataTotalCount % pageSize != 0){
            //有余数，pageTotalCount加一处理
            pageTotalCount++;
        }
        pageDto.setPageTotalCount(pageTotalCount);
        pageDto.setDataTotalCount(dataTotalCount);
        if (currentPage < 1){
            currentPage = 1;
        }
        if (currentPage > pageTotalCount){
            currentPage =pageTotalCount;
        }
        pageDto.setCurrentPage(currentPage);
        //页码pages展示
        pages.add(currentPage);
        for (int i = 1;i <= 3;i++) {
            //往前头部插入,索引0处
            if (currentPage -i > 0){
                pages.add(0,currentPage -i);
            }
            //往后尾部插入
            if(currentPage +i <= pageTotalCount){
                pages.add(currentPage + i);
            }
        }
        //是否展示上一页
        if (currentPage == 1){
            pageDto.setShowPreviousPage(false);
        }else {
            pageDto.setShowPreviousPage(true);
        }
        //是否展示下一页
        if (currentPage == pageTotalCount){
            pageDto.setShowNextPage(false);
        }else {
            pageDto.setShowNextPage(true);
        }
        //是否展示首页
        if(currentPage == 1){
            //页码包含第一页就不展示
            pageDto.setShowFirstPage(false);
        }else{
            pageDto.setShowFirstPage(true);
        }
        //是否展示最后一页
        if (currentPage == pageTotalCount){
            //页码包含最后一页就不展示
            pageDto.setShowLastPage(false);
        }else {
            pageDto.setShowLastPage(true);
        }
    }
}
